
package interfaz;

import dao.MySqlUsuarioDAO;
import datos.Usuario;
import encriptacion.Encrypter;
import encriptacion.VernamEncrypter;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

public class VentanaSesion extends javax.swing.JDialog {
    private static MySqlUsuarioDAO mud = new MySqlUsuarioDAO();
    private VentanaPrincipal vp;

    public VentanaSesion(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        vp = (VentanaPrincipal)parent;
        this.setLocationRelativeTo(null);
        jtfUsuario.addKeyListener(new MakeEnterDoAction()); // convert Enter to an ActionEvent
        jpfContraseña.addKeyListener(new MakeEnterDoAction()); // convert Enter to an ActionEvent
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfUsuario = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbCrear = new javax.swing.JButton();
        jbAceptar = new javax.swing.JButton();
        jpfContraseña = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Usuario:");

        jLabel2.setText("Contraseña:");

        jbCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/Botones/AgregarP.png"))); // NOI18N
        jbCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCrearActionPerformed(evt);
            }
        });

        jbAceptar.setText("Aceptar");
        jbAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAceptarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfUsuario)
                            .addComponent(jpfContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addComponent(jbAceptar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jbCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jpfContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(jbAceptar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAceptarActionPerformed
        if(this.verificarDatos()){
            String pass = new String(jpfContraseña.getPassword());
            Encrypter enc = new VernamEncrypter(pass);
            enc.code();
            pass = enc.getEncrypted();
            Usuario usuario = new Usuario(jtfUsuario.getText(), pass);
            usuario = mud.find(usuario);
            if(usuario != null){
                JOptionPane.showMessageDialog(null, "Login correcto");
                vp.setUsuario(usuario);
                new VentanaNuevaPartida(vp, false).setVisible(true);
                dispose();
            }else{
                JOptionPane.showMessageDialog(null, "El usuario no existe");
            }
        }else{
            JOptionPane.showMessageDialog(null, "No deje casilleros vacios");
        }
    }//GEN-LAST:event_jbAceptarActionPerformed

    private void jbCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCrearActionPerformed
        new VentanaCrearUsuario(null, true).setVisible(true);
    }//GEN-LAST:event_jbCrearActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jbAceptar;
    private javax.swing.JButton jbCrear;
    private javax.swing.JPasswordField jpfContraseña;
    private javax.swing.JTextField jtfUsuario;
    // End of variables declaration//GEN-END:variables

    private boolean verificarDatos() {
        if(jtfUsuario.getText().equalsIgnoreCase("")){
            return false;
        }
        String pass = new String(jpfContraseña.getPassword());
        if(pass.equalsIgnoreCase("")){
            return false;
        }
        return true;
    }


    public class MakeEnterDoAction extends KeyAdapter {

        public void keyPressed(KeyEvent ke)
        {
            if (ke.getKeyCode() == KeyEvent.VK_ENTER)
            {
                Object src = ke.getSource();
                jbAceptarActionPerformed(new ActionEvent(src, ActionEvent.ACTION_PERFORMED, "Enter"));
            }
        } // end keyPressed()

    } // end class
}
